var ParseTemplate = ZFT.Util.ParseTemplate;
var Ajax = ZFT.Util.Ajax;
var STip = ZFT.Util.STip;
var Pagination = require("COMMON/modules/pagination-x/v1.0/");
var Template = {
    wdlist: ParseTemplate(require("./tpl/list.xtpl")),
    detail: ParseTemplate(require("./tpl/detail.xtpl")),

}
require("./index.scss");
var Main = ZFT.Util.Class({
    container: $("body"),
    EVENTS: {
        "click .cancel,.close": "closeModal",
    },
    init: function () {
        var _this = this;
        var pagination = this.pagination = new Pagination({
            container: $(".paginationWrap"),
            count: 5,
            showTotal: true,
            jump: true,
            filter: true
        });
        _this.getDWList(1);
        pagination.on("page.switch", function (toPage, currentPage, totalPage) {
            pagination.render({ current: currentPage, toPage: toPage }),
                _this.getDWList(toPage);
        })
        $("#schWdBtn").on("click", function () {
            _this.getDWList(1);
        })
    },
    dType: ZFT.Util.getDtype(),

    getDWList: function (toPage) {
        var _this = this;
        var tbody = $("#tbody");
        var pagin = $(".paginationWrap");
        var pageSize = pagin.attr("data-size");
        var type = $("#type").val();
        var content = $("#contentInp").val();
        var status = $("#wdStatus").val();
        if (!toPage) toPage = 1;
        Ajax("/r/Withdraw_getList", {
            type: "POST",
            dataType: "json",
            params: {
                type: type,
                content: content,
                status: status,
                page: toPage,
                page_size: pageSize ? pageSize : 10
            },
            loading: function () {
                pagin.hide();
                var loading = '<tr><td colspan="9">加载中,请稍后...</td></tr>'
                tbody.html(loading);
            },
            success: function (res) {
                var data = res.data.list;
                var html;
                if (res.code == 200) {                  
                    var pers=ZFT.Util.getPermission();
                    data["wdPer"]=($.inArray("42",pers)!==-1) ?　1 : 0;
                    data["dtype"] = _this.dType;
                    html = Template.wdlist({ data: data });
                    tbody.html(html);
                    _this.pagination.render({ current: toPage, toPage: toPage, total: res.data.total });
                    _this.viewWDDetail();
                } else if (res.code == 102) {
                    var relogin = '<tr><td colspan="9"><a href="index/logOut">未登录,请重新登录</a></td></tr>'
                    tbody.html(relogin);
                } else {
                    var html = '<tr><td colspan="9">' + res.msg + '</td></tr>'
                    tbody.html(html);
                }
            }
        })
    },
    //查看提现详情
    viewWDDetail: function () {
        var _this = this;
        $("#tbody .btn-view").on("click", function (e) {
            var tarBtn = $(e.currentTarget);
            var id = tarBtn.attr("data-id");
            var status = tarBtn.attr("data-status");
            var html;
            Ajax("/r/Withdraw_getDetail", {
                type: "POST",
                dataType: "json",
                params: {
                    id: id
                },
                success: function (res) {
                    var data = res.data;
                    if (res.code == 200) {
                        data["dtype"] = _this.dType;
                        data["status"] = status;
                        html = Template.detail({ data: res.data });
                        $("#modal").html(html).fadeIn(200);
                        if (_this.dType == 0) {
                            _this.adminCheckWD();
                        }
                    } else {
                        return STip("fail", res.msg, 4000)
                    }

                }
            })

        })
    },
    //管理员审核
    adminCheckWD: function () {
        $("#checkBtn").on("click", function (e) {
            $("#modal").html("");
        })
    },
    //关闭模态框
    closeModal: function (e) {
        var tarBtn = $(e.currentTarget);
        tarBtn.parents(".m-modal_bg").fadeOut(200);
    }
})

$(function () {
    new Main();
})